package com.tb;

import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLInitializationException;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Comment generated by idea
 * Created by zhaoYuan on 2017/8/7.
 */
public class TbTest {

    @org.junit.Test
    public void connPoolTest() throws InterruptedException {
        LayeredConnectionSocketFactory ssl = null;
        try {
            ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
        } catch (final SSLInitializationException ex) {
            final SSLContext sslcontext;
            try {
                sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
                sslcontext.init(null, null, null);
                ssl = new SSLConnectionSocketFactory(sslcontext);
            } catch (final SecurityException ignore) {
            } catch (final KeyManagementException ignore) {
            } catch (final NoSuchAlgorithmException ignore) {
            }
        }

        final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory())
                .build();

        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(sfr);
        connMgr.setDefaultMaxPerRoute(5);
        connMgr.setMaxTotal(10);

        CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connMgr).build();
        final Executor executor = Executor.newInstance(httpClient);


        String url = "http://oc.124zhe.com/crm/member_manage/index";
        final Request request = Request.Get(url)
//                .viaProxy(new HttpHost("59.38.241.134", 23305,"http"))
                .connectTimeout(10000).socketTimeout(10000);

        int requestNum = 2000;
        final CountDownLatch countDownLatch = new CountDownLatch(requestNum);
        final AtomicInteger count = new AtomicInteger(0);
        for(int i=0; i<requestNum; i++){
            final int finalI = i;

            new Thread(new Runnable() {

                public void run() {
                    try {
                        Response response = executor.execute(request);
//                        Response response = request.execute();
                        String result = response.handleResponse(new BasicResponseHandler());
                        System.out.println(result.contains("智能店长"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        countDownLatch.countDown();
                        System.out.println("======"+count.incrementAndGet());
                    }
                }
            }).start();
        }
        countDownLatch.await();
    }
}
